﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangMayTinh.BusinessLogicLayer;

namespace CuaHangMayTinh.PresentationLayer
{
    public partial class FormBoPhan : Form
    {
        private DataTable dtNhomBoPhan = GroupUserManager.Find_All();

        public FormBoPhan()
        {
            InitializeComponent();
        }

        private void FormBoPhan_Load(object sender, EventArgs e)
        {
            LoadListView(dataGridView_NhomBoPhan, dtNhomBoPhan, "TenGU");
        }

        private void dataGridView_NhomBoPhan_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView_NhomBoPhan.SelectedRows.Count > 0)
            {
                var row = dataGridView_NhomBoPhan.SelectedRows[0];
                DataTable subgruser = SubGroupUserManager.Find_IDGroupUser((int)row.Cells[0].Value);
                LoadListView(dataGridView_BoPhan,subgruser,"Name");
            }
            dataGridView_NhanVien.DataSource = null;
        }

        private void dataGridView_BoPhan_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView_BoPhan.SelectedRows.Count > 0)
            {
                var row = dataGridView_BoPhan.SelectedRows[0];
                DataTable nhanvien = NhanVienManager.Find_SubGroupUser((int)row.Cells[0].Value, false);
                LoadListView(dataGridView_NhanVien,nhanvien,"TenNV");
            }
        }

        private void LoadListView(DataGridView dataGrid, DataTable dt,string ten)
        {
            if (dt != null&&dt.Rows.Count>0)
            {
                dt.Columns.Add("STT");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["STT"] = i + 1;
                }
                dataGrid.DataSource = dt;
                dataGrid.Columns["STT"].DisplayIndex = 0;
                for (int i = 0; i < dataGrid.Columns.Count; i++)
                {
                    dataGrid.Columns[i].Visible = false;
                }
                dataGrid.Columns["STT"].Visible = true;
                dataGrid.Columns[ten].Visible = true;
            }
            else
            {
                dataGrid.DataSource = null;
            }
        }
    }
}
